package com.linc.facerecongnize;

import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.android.widget.demo.R;

public class FaceDetectActivity extends AppCompatActivity {
    private final static String TAG = "FaceDetectActivity";
    private static final int OPEN_PHOTO_FOLDER_REQUEST_CODE = 9998;

    private FDView mFRView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_recongnize);
        mFRView = (FDView)findViewById(R.id.frview);
    }

    public void onSelectPhotoClicked(View view) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, OPEN_PHOTO_FOLDER_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(OPEN_PHOTO_FOLDER_REQUEST_CODE == requestCode && RESULT_OK == resultCode) {
            Uri uri = data.getData();
            try {
                Log.d(TAG,uri.getPath());
                Log.d(TAG,"width "+mFRView.getMeasuredWidth()+" width is "+
                        mFRView.getWidth());
//                mFRView.initBitmap(uri, mFRView.getWidth(), mFRView.getHeight());

                initFRViewWithPath(uri);

            } catch (Exception ex) {
                Log.e(TAG, "failed." + ex.getMessage());
            }
        }
    }

    private void initFRViewWithPath(Uri uri) {
        String[] projection = {MediaStore.Images.Media.DATA};
//        Cursor cursor = managedQuery(uri, projection, null, null, null);//deprecated
        CursorLoader cursorLoader = new CursorLoader(this,uri,projection,null,null,null);
        Cursor cursor = cursorLoader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();

        String path = cursor.getString(column_index);
        Log.e(TAG,"real path: "+path);
        mFRView.initBitmap(path,mFRView.getWidth(),mFRView.getHeight());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_face_recongnize, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
